<template>
  <div id="login">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="demo-ruleForm">
      <el-form-item prop="username">
        <h2 style="color:white;">自行车租赁平台登录</h2>
        <el-input
          type="text"
          placeholder="请输入账号"
          prefix-icon="el-icon-user-solid"
          v-model="ruleForm.phone"
          size="small"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          placeholder="请输入密码"
          v-model="ruleForm.password"
          prefix-icon="el-icon-user-solid"
          size="small"
        ></el-input>
      </el-form-item>
      <el-form-item class="loginbtn">
        <el-button type="primary" @click="loginFrom">注册并登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { userLogin } from "../../../api";
import config from "../../../config/index";
import { check_phone } from "../../../common/js/myFun";

export default {
  name: "Login",
  data() {
    return {
      checked: false,
      ruleForm: {
        phone: "",
        password: ""
      },
      rules: {
        phone: [{ required: true, message: "账号不能为空", trigger: "blur" }],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    async loginFrom() {
      if (!check_phone(this.ruleForm.phone)) {
        this.$message({
          message: "请输入合法手机号码",
          type: "error"
        });
        return;
      }
      let user = await userLogin(this.ruleForm);
      if (user.code === 200) {
        let userInfo = JSON.stringify(user.result.userInfo);
        config.setCache("userInfo", userInfo);
        await this.$store.dispatch("addUserInfo2", {
          userInfo: user.result.userInfo
        });
        await this.$router.push({ path: "/user/home" });
        this.$message({
          message: user.data,
          type: "success"
        });
      } else {
        // this.$message.error(user.data);
        this.$message({
          message: user.data,
          type: "error"
        });
      }
    }
  }
};
</script>

<style scoped lang="stylus" ref="stylesheet/stylus">
#login {
  width: 100vw;
  height: 100vh;
  background-color: #303643;
  display: flex;
  align-items: center;
  justify-content: center;
}

.el-input {
  width: 400px;
}

.loginbtn {
  text-align: center;
}

.el-button {
  text-align: center;
  width: 100%;
}
</style>
